package com.bitter.order.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.bitter.fegin.StorageFeginApi;
import com.bitter.order.common.exception.OrderException;
import com.bitter.order.domain.OrderVo;
import com.bitter.order.mapper.OrderMapper;

import io.seata.spring.annotation.GlobalTransactional;

@Service("orderServiceImpl")
public class OrderServiceImpl implements IOrderService {

	@Autowired
	OrderMapper orderMapper;

	@Autowired
	StorageFeginApi storageApi;

	@Override
	public String addBySeata(OrderVo orderVo) {
		// TODO Auto-generated method stub
		orderMapper.insert(orderVo);

		return "";
	}

	/**
	 * 创建订单 测试结果： 1.添加本地事务：仅仅扣减库存<br/>
	 * 2.不添加本地事务：创建订单，扣减库存
	 * 只能在同一個事務里面去操作
	 */
	@Override
	@GlobalTransactional(name = "sos-create-order", rollbackFor = Exception.class)
	public int create(OrderVo orderVo) {
		orderMapper.insert(orderVo); // 创建订单
		
		storageApi.decrease(orderVo.getProduct_id(), orderVo.getTotal());
		
		if (orderVo.getTotal() == 1000) {
            throw new RuntimeException("异常回滚");
        }
		return 0;
	}
}
